<template>
<view class="register" :style="{height:wh+'px'}">
		<view class="title">
			<image src="../../static/images/register.png" mode="aspectFit"></image>
			<image src="../../static/images/register2.png" mode="aspectFit"></image>
		</view>
		<view class="content">
			<uni-forms ref="form" :rules="rules" :model="regForm" validateTrigger="blur">
				<uni-forms-item name="username">
					<uni-easyinput prefix-icon="person" v-model="regForm.username" placeholder="请输入用户名" />
				</uni-forms-item>
				<uni-forms-item name="password">
					<uni-easyinput prefix-icon="locked" type="password" v-model="regForm.password"
						placeholder="请输入密码" />
				</uni-forms-item>
			
				<uni-forms-item name="mobilePhoneNumber">
					<uni-easyinput prefix-icon="phone" type="number" v-model="regForm.mobilePhoneNumber" placeholder="请输入手机" />
				</uni-forms-item>
			</uni-forms>
			<button class="button" type="primary" @click="register(form)">注册</button>
		</view>
		<view class="login">
			<navigator url="/pages/login/login">已有账号，去登录</navigator>
		</view>
	</view>

</template>

<script setup>
	import {
		onLoad
	} from "@dcloudio/uni-app"
	import {
		ref,
		computed
	} from 'vue'

	import {
		userStore
	} from "../../store/userstore.js"
	import {
		CONFIG
	} from "../../utils/config.js"
	import {
		onReady
	} from "@dcloudio/uni-app"
	const regForm = ref({
		username: '',
		password: '',
		// password2: '',
		// email: '',
		mobilePhoneNumber: ''
	})
	const form = ref(null)

	function toLogin() {
		uni.navigateTo({
			url: "/pages/login/login"
		})
	}
	const wh = ref(0)
	onReady(() => {
		// wh.value=uni.getSystemInfo().windowHeight
		// console.log(uni.getSystemInfo().windowHeight);
		wh.value = uni.getWindowInfo().windowHeight
		console.log(uni.getWindowInfo().windowHeight)
	})

	function register(form) {
		form.validate().then(res => {
			//验证通过,进行接口请求用户注册
			uni.$post(`${CONFIG.leanCloud_url}/1.1/users?`, regForm.value).then(res2 => {
				//返回码400表示注册失败。显示错误信息
				if (res2.code == 400) {
					uni.showToast({
						title: res2.message,
						icon: "error"
					})
					return;
				}
				// 返回吗200表示注册成功，显示显示成功信息，并返回
				if (res2.code == 200) {
					uni.showToast({
						title: "登录成功",
						icon: "success"
					})
					// uni.navigateTo({
					// 	url:"/pages/Login/Login"
					// })


				}
				uni.navigateBack()
			})
		}).catch(err => {
			// 验证失败
			console.log(err);
		})
	}

	const rules = ref({
		username: {
			rules: [{
					required: true,
					errorMessage: "请输入用户名"
				},
				{
					pattern: "^\\w{3,10}$",
					errorMessage: '请输入3-10位数字字母和下划线作为用户名'
				}
			]
		},
		password: {
			rules: [{
					required: true,
					errorMessage: "请输入密码"
				},
				{
					pattern: '^[a-zA-Z0-9._!@#$%^&]{3,10}$',
					errorMessage: '请输入3-10位数字字母和特殊字符密码'
				}
			]
		},
		
		mobilePhoneNumber: {
			rules: [{
				required: true,
				errorMessage: "请输入手机号码"
			}, {
				pattern: '^1[3-9]\\d{9}$',
				errorMessage: "电话号码格式错误"
			}]
		}
		
	})
</script>


<style>

</style>
